<?php
class CategoryProductsController extends AppController {

	var $name = 'CategoryProducts';
	var $uses= array('Category');
	function index() {
		$lang=$this->Session->read('lang');
		$lang=0;
		$this->Category->recursive = 0;
		$conditions=array('Category.lang_id'=>$lang,'Category.type'=>1,'Category.parent_id <>'=>NULL);
		$this->paginate = array('conditions' =>$conditions,'order'=>'Category.id DESC','limit' =>20);
        $Categorys = $this->paginate('Category');
		$this->set('Categorys', $Categorys);	
	}
	
	function add() {
		$lang=$this->Session->read('lang');
		$lang=0;
		if (!empty($this->data)) {
			$this->Category->create();
			$this->data['Category']['type']=1;
			$this->data['Category']['lang_id']=$lang;
			$alias=strtolower(khongdau($this->data['Category']['name']));
			$this->data['Category']['alias']= Inflector::slug($alias,'-');
			if ($this->Category->save($this->data)) {
				if($this->data['Category']['check']==1){
					$this->Session->setFlash(__('Thêm mới danh mục thành công', true));
					$this->redirect(array('action' => 'add'));
				}else{
					$this->Session->setFlash(__('Thêm mới danh mục thành công', true));
					$this->redirect(array('action' => 'index'));
				}
			} else {
				$this->Session->setFlash(__('Thêm mơi danh mục thất bại. Vui long thử lại', true));
			}
		}
		$this->loadModel("Category");
		$conditions=array('Category.type'=>1,'Category.lang_id'=>$lang);
        $categorylist  = $this->Category->generatetreelist($conditions,null,null," _ ");		
		$this->set(compact('categorylist'));

	}	
	function edit($id=null) {
		//$lang=$this->Session->read('lang');
		$lang=0;
		if (!empty($this->data)) {
			$alias=strtolower(khongdau($this->data['Category']['name']));
			$this->data['Category']['alias']= Inflector::slug($alias,'-');
			$this->data['Category']['lang_id']=$lang;
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__('Sửa thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không sửa được vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Category->read(null, $id);
		}
		$this->loadModel("Category");
		$conditions=array('Category.type'=>1,'Category.lang_id'=>$lang);
        $categorylist  = $this->Category->generatetreelist($conditions,null,null," _ ");		
		$this->set(compact('categorylist'));

	}
	
	function delete() {
		if($_POST['chkId']){
			$Idnews=$_POST['chkId'];
			foreach($Idnews as $id){
				$this->Category->delete($id);			
			}
			$this->Session->setFlash(__('Bạn Delete thành công', true));
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name);
		}

	}

	
}
?>